package comqyf.nacoscloudzookeeper.repository;


import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ZooRepository {

    @Autowired
    private CuratorFramework client;

    private static final String BASE_PATH = "/qyf/";
    private static final String BASE_PATH_SUFFIX = "/qyf";
    /**
     * 创建空节点
     * @param path
     * @return
     * @throws Exception
     */
    public String createNode(String path) throws Exception{
        String name = client.create().forPath(BASE_PATH + path);
        return name;
    }

    /**
     * 创建节点
     * @param path
     * @param value
     * @return
     * @throws Exception
     */
    public void createNode(String path, String value) throws Exception {
        if (checkNode(path) != null){
            client.setData().forPath(BASE_PATH + path, value.getBytes());
        }else {
            client.create().forPath(BASE_PATH + path, value.getBytes());
        }
    }

    /**
     * 创建临时节点
     * @param path
     * @throws Exception
     */
    public void createWithModeNode(String path) throws Exception {
        client.create().withMode(CreateMode.EPHEMERAL).forPath(BASE_PATH + path);
    }

    /**
     * 创建临时节点
     * @param path
     * @param value
     * @throws Exception
     */
    public void createWithModeNode(String path, String value) throws Exception {
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(BASE_PATH + path, value.getBytes());
    }

    /**
     * 获取节点数据
     * @param path
     * @return
     * @throws Exception
     */
    public String getNode(String path) throws Exception {
        byte[] bytes = client.getData().forPath(BASE_PATH + path);
        return new String(bytes);
    }

    /**
     * 填充节点数据
     * @param path
     * @param value
     * @throws Exception
     */
    public void setNode(String path, String value) throws Exception {
        client.setData().forPath(BASE_PATH + path, value.getBytes());
    }

    /**
     * 检查节点是否存在
     * @param path
     * @return
     * @throws Exception
     */
    public Stat checkNode(String path) throws Exception {
        Stat stat = client.checkExists().forPath(BASE_PATH + path);
        return stat;
    }

    /**
     * 删除节点
     * @param path
     * @throws Exception
     */
    public void delNode(String path) throws Exception {
        if (checkNode(path) != null){
            client.delete().guaranteed().deletingChildrenIfNeeded().forPath(BASE_PATH + path);
        }
    }

    /**
     * 获取所有子节点
     * @return
     * @throws Exception
     */
    public List<String> getChildPaths() throws Exception {
        List<String> list = new ArrayList<>();
        List<String> nodes = client.getChildren().forPath(BASE_PATH_SUFFIX);
        if (nodes != null && !nodes.isEmpty()){
            for (String _node : nodes){
                String nodePath = BASE_PATH_SUFFIX + "/" + _node;
                list.add(nodePath);
            }
        }
        return list;
    }

    /**
     * 获取所有子节点数据
     * @return
     * @throws Exception
     */
    public Map<String, String> getChildValues() throws Exception {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        List<String> listPath = this.getChildPaths();
        for (String childPath : listPath){
            String value = new String(client.getData().forPath(childPath));
            map.put(childPath, value);
        }
        return map;
    }
}
